<template>
  <div class="app-container">
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="800px"
      :close-on-click-modal="false"
      :show-close="true"
      append-to-body
      :before-close="handleClose"
    >
      <div slot="title">
        {{ title }}
      </div>
      <div style="height: 350px">
        <!-- 搜索工具栏 -->
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
          <el-form-item label="客户名称" prop="search">
            <el-input v-model="queryParams.search" placeholder="请输入客户名称"
                      clearable style="width: 240px" @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <!-- 表格 -->
        <el-table ref="customerTable" highlight-current-row :data="tableData" style="width: 100%"
                  @selection-change="handleSelectionChange" height="265"
        >
          <el-table-column type="selection" width="55" align="center"/>
          <el-table-column show-overflow-tooltip align="center" fixed prop="customerName" label="客户名称"
                           min-width="135"
          >
            <template slot-scope="scope">
              <el-button type="text">{{ scope.row.customerName }}</el-button>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip align="center" prop="sourceId" label="客户来源" min-width="155">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.crm_source_name" :value="scope.row.sourceId"/>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip align="center" prop="mobile" label="手机" min-width="155"/>
          <el-table-column show-overflow-tooltip align="center" prop="telephone" label="电话" min-width="175"/>
          <el-table-column show-overflow-tooltip align="center" prop="website" label="网址" min-width="175"/>
          <el-table-column show-overflow-tooltip align="center" prop="email" label="邮箱" min-width="95"/>
          <el-table-column show-overflow-tooltip align="center" prop="industryId" label="客户行业" min-width="95">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.crm_customer_industry" :value="scope.row.industryId"/>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip align="center" prop="levelId" label="客户级别" min-width="155">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.crm_customer_level" :value="scope.row.levelId"/>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip align="center" prop="remark" label="备注" min-width="155"/>
          <el-table-column show-overflow-tooltip align="center" prop="dealStatus" label="成交状态" min-width="175">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.crm_customer_deal_status" :value="scope.row.dealStatus"/>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip align="center" prop="address" label="省市区" min-width="115"/>
          <el-table-column show-overflow-tooltip align="center" prop="detailAddress" label="详细地址" min-width="155"/>
        </el-table>
        <!-- 分页条 -->
        <pagination
          v-show="total>0"
          :total="total"
          :page-sizes="[15,30,50,100]"
          :page.sync="queryParams.page"
          :limit.sync="queryParams.limit"
          @pagination="getList"
        />
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" @click="submitForm">保 存</el-button>
        <el-button size="small" @click="handleClose">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listByParamsForTable } from '@/api/crm/Customer'

export default {
  name: 'CustomerSelectPopover',
  dicts: ['crm_source_name', 'crm_customer_industry', 'crm_customer_level', 'crm_customer_deal_status'],
  components: {},
  props: {},
  data() {
    return {
      dialogVisible: false,
      title: '关联客户模块',
      tableData: [],
      // 查询参数
      queryParams: {
        page: 1,
        limit: 15,
        search: '',
        searchId: null
      },
      total: 0,
      selectData: null,
      customerId: null
    }
  },
  // 在实例创建完成后被立即调用
  created() {
  },
  // 实例被挂在后调用
  mounted() {

  },
  // 方法
  methods: {
    // 查询数据
    getList() {
      listByParamsForTable(this.queryParams).then(res => {
        this.tableData = res.data.records
        this.total = res.data.totalCount
      })
    },
    // 查询
    handleQuery() {
      this.getList()
    },
    open() {
      this.dialogVisible = true
      listByParamsForTable(this.queryParams).then(res => {
        this.tableData = res.data.records
        this.total = res.data.totalCount
      })
    },
    handleClose() {
      this.dialogVisible = false
    },
    // 重置
    resetQuery() {
      this.reset()
      this.getList()
    },
    reset() {
      this.queryParams = {
        page: 1,
        limit: 15,
        data: {}
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.customerTable.clearSelection()
        this.$refs.customerTable.toggleRowSelection(selection.pop())
        return
      }
      if (selection[0]) {
        this.selectData = selection[0]
      } else {
        this.selectData = null
      }
    },
    submitForm() {
      this.handleClose()
      this.$emit('callback', this.selectData)
    }
  }
}
</script>

<style scoped>

</style>
